package com.hutao.chargingpile.pitfall.controller;

import com.alibaba.excel.EasyExcel;
import com.hutao.chargingpile.pitfall.pojo.dto.ExcelCpHitchDTO;
import com.hutao.chargingpile.pitfall.pojo.entity.CpHitch;
import com.hutao.chargingpile.pitfall.pojo.query.CpHitchQuery;
import com.hutao.chargingpile.pitfall.result.R;
import com.hutao.chargingpile.pitfall.service.CpHitchManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 故障表 前端控制器
 * </p>
 *
 * @author HuTao
 * @since 2023-03-22
 */
@Slf4j
@Api(tags = "故障管理")
@RestController
//@CrossOrigin
@RequestMapping("/admin/pitfall/cpHitch")
public class CpHitchManagerController {

    @Resource
    private CpHitchManagerService cpHitchManagerService;

    @ApiOperation("查询所有故障信息")
    @GetMapping("/findAllCpHitch/{page}/{limit}")
    public R findAllCpHitch(@PathVariable Integer page,
                            @PathVariable Integer limit,
                            CpHitchQuery cpHitchQuery) {
        Integer status = cpHitchQuery.getStatus();
        String startTime = cpHitchQuery.getStartTime();
        String endTime = cpHitchQuery.getEndTime();
        String addressSite = cpHitchQuery.getAddressSite();
        Map<String, Object> map = new HashMap<>();
        map.put("page", (page - 1) * limit);
        map.put("limit", limit);
        map.put("status", status);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("addressSite", addressSite);
        int count = cpHitchManagerService.findCount(map);
        return R.ok().datas("list", cpHitchManagerService.findAllHitch(map), count);
    }

    @ApiOperation("Excel导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        //这里注意，有同学使用swagger会导致各种问题，请直接使用浏览器或postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName= URLEncoder.encode("故障管理","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), ExcelCpHitchDTO.class).sheet("故障管理").doWrite(cpHitchManagerService.listCpHitchData());
    }

    @ApiOperation("单删")
    @DeleteMapping("/removeById/{id}")
    public R removeById(@PathVariable Long id){
        boolean result = cpHitchManagerService.removeById(id);
        if(result){
            return R.ok().message("逻辑删除成功!!!");
        }else{
            return R.error().message("逻辑删除失败!!!");
        }
    }

    @ApiOperation("批量逻辑删除")
    @DeleteMapping("/removeIds")
    public R removeIds(@RequestBody Integer[] idList){
        System.out.println(idList);
        //数组转集合
        List<Integer> ids = Arrays.asList(idList);
        int result = cpHitchManagerService.removeIds(ids);
        if(result>0){
            return R.ok().message("批量逻辑删除成功");
        }else{
            return R.ok().message("批量逻辑删除失败");
        }
    }

    @ApiOperation("添加故障信息")
    @PostMapping("/addCpHitchManager")
    public R addCpHitchManager(@RequestBody CpHitch cpHitch){
        int result = cpHitchManagerService.addCpHitchManager(cpHitch);
        if(result>0){
            return R.ok().message("添加成功!");
        }else{
            return R.ok().message("添加失败!");
        }
    }
}
